//
//  NowCard.swift
//  MovieAppDemo
// 
//  Created by Demo on 2025/1/25.
//

import SwiftUI
import UIKit
import Kingfisher
import ColorThiefSwift


struct NowCard: View {
    
    var item:MainPica = mainpicas[0]
    @State var isPressed: Bool = false
    @State private var dominantColor: Color = Color("BgColor1")  // 初始值，后面会更新
    
    var body: some View {
        ZStack {
            CachedImageView2(imageURL: item.cover,dominantColor:$dominantColor)//这是封面
                .clipShape(RoundedRectangle(cornerRadius: 10.0))
        }
        .overlay(alignment: .topLeading) {
            Text(item.tags[0])
                .font(.system(size: 16, weight: .medium))
                .foregroundColor(.white)
                .padding(.horizontal, 6)  // 调整水平 padding，增加左右间距
                .padding(.vertical, 4)    // 调整垂直 padding，减少上下间距
                .background(dominantColor)
                .clipShape(RoundedCorners(corners: [.bottomRight], radius: 10))  // 只应用右下角圆角
                .offset(x: 2, y: 2)  // 调整标签位置，偏移到卡片的左上角
        }
        
        .overlay(alignment: .topLeading) {
            VStack{
                Spacer()
                VStack(alignment: .leading) {
                    Text("\(item.fav_time)")
                        .font(.system(size: 16, weight: .medium))
                        .padding(.horizontal,10)
                        .lineLimit(1)
                        .multilineTextAlignment(.leading)  // 确保多行文本左对齐
                        .foregroundColor(.white).opacity(0.8)
                    
                    Text("\(item.name)")
                        .font(.system(size: 30, weight: .black))
                        .padding(.horizontal,10)
                        .lineLimit(1)
                        .foregroundStyle(.white)
                        .frame(maxWidth: 300, alignment: .leading)
                    Text("\(item.desc)")
                        .font(.system(size: 16, weight: .medium))
                        .frame(maxWidth: .infinity, alignment: .leading)  // 设置最大宽度并左对齐
                        .foregroundColor(.white).opacity(0.8)
                        .padding(.horizontal, 10)
                        .lineLimit(3)
                        .multilineTextAlignment(.leading)  // 确保多行文本左对齐
                    
                    HStack{
                        CachedImageView(imageURL: item.cover)
                            .frame(width: 50,height:50)
                            .clipShape(RoundedRectangle(cornerRadius: 10.0))
                        VStack{
                            Text("当前进度:")
                                .font(.system(size: 20, weight: .medium))
                                .frame(maxWidth: .infinity, alignment: .leading)  // 设置最大宽度并左对齐
                                .foregroundColor(.white)
                                .padding(.horizontal, 10)
                            Text("第一话")
                                .font(.system(size: 18, weight: .medium))
                                .frame(maxWidth: .infinity, alignment: .leading)  // 设置最大宽度并左对齐
                                .foregroundColor(.white).opacity(0.8)
                                .padding(.horizontal, 10)
                        }
                        HStack {
                            Image(systemName: "play.fill")
                                .font(.system(size: 20, weight: .medium))
                            Text("继续阅读")
                                .font(.system(size: 20, weight: .medium))
                        }.frame(maxWidth: .infinity)
                        
                            .padding(10)
                            .foregroundColor(.black)
                            .background(.white)
                            .clipShape(RoundedRectangle(cornerRadius: 10.0))
                    }
                    .padding(10)
                    
                    .background(dominantColor)
                }
                .background(
                    LinearGradient(
                        gradient: Gradient(colors: [dominantColor.opacity(0), dominantColor.opacity(0.8), dominantColor.opacity(1)]),//怎么把这里的渐变改成封面图片的主色调的渐变
                        startPoint: .top,
                        endPoint: .bottom
                    )
                )
                
            }
        }
        
        .clipShape(RoundedRectangle(cornerRadius: 10.0))
        .overlay(
            RoundedRectangle(cornerRadius: 10.0)
                .stroke(dominantColor, lineWidth: 5)  // 5像素的灰色边框
        )
        .contentShape(Rectangle())  // 确保点击区域覆盖整个视图
        
        .scaleEffect(isPressed ? 0.95 : 1)
        .simultaneousGesture(
            DragGesture(minimumDistance: 0)
                .onChanged { _ in
                    withAnimation(.easeInOut(duration: 0.2)) {
                        isPressed = true
                        
                    }
                }
                .onEnded { _ in
                    withAnimation(.easeInOut(duration: 0.2)) {
                        isPressed = false
                    }
                }
        )
        
    }
    
 
    
}

#Preview {
    NowCard()
        .padding()
        .background(Color("BgColor1"))
        .frame(height:400)
    
}
